<?php

namespace app\index\controller;
use think\Request;
use think\Session;
//http://dev.91t.com/index/cart/index.html
use app\index\controller\User;
use think\Db;

class Cart extends User
{
    // 购物车页面
    public function index()
    {
        $this->assign('cart_title', '我的购物车');

        // 加载商品价格策略

        // 购物车数据
        $list = model('Cart')->getCartData($this->member);
        $this->assign('list', $list);
        //dump($list);exit;

        return $this->fetch('trade/cart/cart');
    }

    // 添加到购物车
    public function add()
    {}
    
    // 删除购物车商品
    public function del()
    {
        $id = input('get.id', 0);
        $rs = model('Cart')->delCartData($id);
        if($rs){
            $data = ['error'=>0, 'msg'=>''];
        }else{
            $data = ['error'=>1, 'msg'=>''];
        }
        return json($data);
    }

    // 清空购物车
    public function emptying()
    {}

    //提交购物车
    public function submit()
    {
        //安全验证

        if (Request::instance()->isPost())
        {
            $post = Request::instance()->post();
            $goodsId = isset($post['goodsId']) ? $post['goodsId'] : '';
            if(empty($goodsId))
            {
                $data = ['error'=>1, 'msg'=>'数据异常'];
            }else{
                //结算的数据保存成session
                Session::set('goodsId', $goodsId);
                //echo Session::get('goodsId');exit;
                $data = ['error'=>0, 'url'=>url('/index/trade/confirm')];
            }
            return json($data);
        }
    }
}